package com.hgyc.mom.qms.service.impl;

import com.hgyc.mom.tool.strategy.AutoCodeUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.qms.mapper.QmsTemplateMapper;
import com.hgyc.mom.qms.entity.QmsTemplate;
import com.hgyc.mom.qms.entity.QmsTemplateIndex;
import com.hgyc.mom.qms.service.QmsTemplateIndexService;
import com.hgyc.mom.qms.entity.QmsTemplateMaterial;
import com.hgyc.mom.qms.service.QmsTemplateMaterialService;
import com.hgyc.mom.qms.vo.QmsTemplateVO;
import com.hgyc.mom.qms.service.QmsTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import com.hgyc.mom.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 检测模板Service业务层实现
 *
 * @author fwj
 * @since 2025-09-26
 */
@Slf4j
@Service
public class QmsTemplateServiceImpl extends ServiceImpl<QmsTemplateMapper, QmsTemplate> implements QmsTemplateService {

    @Autowired
    private QmsTemplateIndexService qmsTemplateIndexService;
    @Autowired
    private QmsTemplateMaterialService qmsTemplateMaterialService;

    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTemplate(QmsTemplateVO qmsTemplateVO) {
        qmsTemplateVO.setTemplateCode(autoCodeUtil.genSerialCode("qms_template_code", null));
        save(qmsTemplateVO);

        //新增检测模板-检测项
        List<QmsTemplateIndex> templateIndexs = qmsTemplateVO.getAddTemplateIndexs();
        if (!CollectionUtils.isEmpty(templateIndexs)) {
            templateIndexs.forEach(item -> {
                item.setId(null);
                item.setTemplateId(qmsTemplateVO.getId());
            });
            qmsTemplateIndexService.saveBatch(templateIndexs);
        }
        //新增检测模板-物料
        List<QmsTemplateMaterial> templateMaterials = qmsTemplateVO.getAddTemplateMaterials();
        if (!CollectionUtils.isEmpty(templateMaterials)) {
            templateMaterials.forEach(item -> {
                item.setId(null);
                item.setTemplateId(qmsTemplateVO.getId());
            });
            qmsTemplateMaterialService.saveBatch(templateMaterials);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTemplate(QmsTemplateVO qmsTemplateVO) {
        // TODO 其他逻辑
        updateById(qmsTemplateVO);

        //删除检测模板-检测项
        if (!CollectionUtils.isEmpty(qmsTemplateVO.getDeleteTemplateIndexIds())) {
            qmsTemplateIndexService.removeBatchByIds(qmsTemplateVO.getDeleteTemplateIndexIds());
        }

        //新增检测模板-检测项
        List<QmsTemplateIndex> templateIndexs = qmsTemplateVO.getAddTemplateIndexs();
        if (!CollectionUtils.isEmpty(templateIndexs)) {
            templateIndexs.forEach(item -> {
                item.setId(null);
                item.setTemplateId(qmsTemplateVO.getId());
            });
            qmsTemplateIndexService.saveBatch(templateIndexs);
        }

        //更新检测模板-检测项
        if (!CollectionUtils.isEmpty(qmsTemplateVO.getUpdateTemplateIndexs())) {
            List<QmsTemplateIndex> updateTemplateIndexs = qmsTemplateVO.getUpdateTemplateIndexs();
            qmsTemplateIndexService.updateBatchById(updateTemplateIndexs);
        }
        //删除检测模板-物料
        if (!CollectionUtils.isEmpty(qmsTemplateVO.getDeleteTemplateMaterialIds())) {
            qmsTemplateMaterialService.removeBatchByIds(qmsTemplateVO.getDeleteTemplateMaterialIds());
        }

        //新增检测模板-物料
        List<QmsTemplateMaterial> templateMaterials = qmsTemplateVO.getAddTemplateMaterials();
        if (!CollectionUtils.isEmpty(templateMaterials)) {
            templateMaterials.forEach(item -> {
                item.setId(null);
                item.setTemplateId(qmsTemplateVO.getId());
            });
            qmsTemplateMaterialService.saveBatch(templateMaterials);
        }

        //更新检测模板-物料
        if (!CollectionUtils.isEmpty(qmsTemplateVO.getUpdateTemplateMaterials())) {
            List<QmsTemplateMaterial> updateTemplateMaterials = qmsTemplateVO.getUpdateTemplateMaterials();
            qmsTemplateMaterialService.updateBatchById(updateTemplateMaterials);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeTemplate(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除检测模板-检测项
        qmsTemplateIndexService.deleteBatchByParentIds(ids);
        //删除检测模板-物料
        qmsTemplateMaterialService.deleteBatchByParentIds(ids);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<QmsTemplate> qmsTemplateList, boolean updateSupport) {
        if (qmsTemplateList == null || qmsTemplateList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + qmsTemplateList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + qmsTemplateList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("检测模板导入失败", e);
            throw new BizException("检测模板导入失败：" + e.getMessage());
        }
    }
}
